<?php

namespace app\store\controller;

use app\store\model\Order as OrderModel;

/**
 * 订单管理
 * Class Order
 * @package app\store\controller
 */
class Order extends Controller
{
    /**
     * 待发货订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function delivery_list()
    {
        return $this->getList('待发货订单列表', [
            'pay_status' => 20,
            'delivery_status' => 10
        ] , 1);
    }

    /**
     * 待收货订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function receipt_list()
    {
        return $this->getList('待收货订单列表', [
            'pay_status' => 20,
            'delivery_status' => 20,
            'receipt_status' => 10
        ] , 2);
    }

    /**
     * 待付款订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function pay_list()
    {
        return $this->getList('待付款订单列表', ['pay_status' => 10, 'order_status' => 10] , 3);
    }

    /**
     * 已完成订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function complete_list()
    {
		$type = 4;
        return $this->getList('已完成订单列表', ['order_status' => 30] , $type);
    }

    /**
     * 已取消订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function cancel_list()
    {
        return $this->getList('已取消订单列表', ['order_status' => 20] , 5 );
    }

    /**
     * 全部订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function all_list()
    {
        return $this->getList('全部订单列表',[],99);
    }

    /**
     * 订单列表
     * @param $title
     * @param $filter
     * @return mixed
     * @throws \think\exception\DbException
     */
    private function getList($title, $filter = [] , $type)
    {
        $model = new OrderModel;
        $list = $model->getList($filter);
        return $this->fetch('index', compact('title','list','type'));
    }

    /**
     * 订单详情
     * @param $order_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail($order_id)
    {
        $detail = OrderModel::detail($order_id);
        return $this->fetch('detail', compact('detail'));
    }

    /**
     * 确认发货
     * @param $order_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delivery($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->delivery($this->postData('order'))) {
            return $this->renderSuccess('发货成功');
        }
        $error = $model->getError() ?: '发货失败';
        return $this->renderError($error);
    }
	
	/**
	*	订单导出
	*/
	public function excelOrder( $type ){
		switch( $type ){
			case 1: //待发货
				$filter = ['pay_status' => 20,'delivery_status' => 10];
				break;
			case 2: //待收货
				$filter = ['pay_status' => 20,'delivery_status' => 20,'receipt_status' => 10];
				break;
			case 3: //待付款
				$filter = ['pay_status' => 10, 'order_status' => 10];
				break;
			case 4: //已完成
				$filter = ['order_status' => 30];
				break;
			case 5: //已取消
				$filter = ['order_status' => 20];
				break;
			default:
				$filter = [];
				break;
		}
		
		$model = new OrderModel;
        $list = $model->getListAll($filter)->toArray();

		$headTitle = '订单列表';
		$title = '订单列表'.'.xls';
		$headTitle = "<tr style='height:50px;border-style:none;><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>";
		$titlename = "<tr>
			<th>订单号</th>
			<th>商品名称</th>
			<th>单价</th>
			<th>实付款</th>
			<th>买家名称</th>
			<th>买家openid</th>
			<th>订单时间</th>
		</tr>";
        self::excelData($list,$titlename,$headTitle,$title);
		die;
	}
	
	
	/* 
	*处理Excel导出 
	*@param $datas array 设置表格数据 
	*@param $titlename string 设置head 
	*@param $title string 设置表头 
	*/
	public static function excelData($datas,$titlename,$title,$filename){

        $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";

        $str .="<table border=1>";
		$str .= "<tr>
			<th>订单号</th>
			<th>商品名称</th>
			<th>单价</th>
			<th>数量</th>
			<th>实付款</th>
			<th>买家名称</th>
			<th>买家id</th>
			<th>订单时间</th>
			<th>付款状态</th>
		</tr>";
        foreach ($datas as $key=> $rt )
        {
			foreach( $rt['goods'] as $gk=>$gv ){
                $str .= "<tr>";
				$count = count( $rt['goods'] );
				if( $gk == '0' ){
					$str .= "<td rowspan='{$count}'>{$rt['order_no']}</td>";
				}
                $str .= "<td>{$gv['goods_name']}</td>";
                $str .= "<td>{$gv['goods_price']}</td>";
                $str .= "<td>{$gv['total_num']}</td>";
                $str .= "<td>{$rt['pay_price']}</td>";
				if( empty( $rt['user']['realName'] ) ){
					$rt['user']['realName'] = $rt['user']['nickName'];
				}
                $str .= "<td>{$rt['user']['realName']}</td>";
                $str .= "<td>{$rt['user']['user_id']}</td>";
                $str .= "<td>{$rt['create_time']}</td>";
                $str .= "<td>{$rt['pay_status']['text']}</td>";
                $str .= "</tr>\n";
			}
        }
        $str .= "</table></body></html>";
        header( "Content-Type: application/vnd.ms-excel; name='excel'" );
        header( "Content-type: application/octet-stream" );
		header( "Content-Disposition: attachment; filename=".$filename );
        header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
        header( "Pragma: no-cache" );
        header( "Expires: 0" );
        exit( $str );
	}


}
